package com.sunday.aomicaichang.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.sunday.common.base.BaseActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.MD5Utils;
import com.sunday.common.utils.SharePerferenceUtils;
import com.sunday.common.utils.ToastUtils;
import com.sunday.common.widgets.CircleImageView;
import com.sunday.aomicaichang.R;
import com.sunday.aomicaichang.http.ApiClient;
import com.sunday.aomicaichang.model.Score;
import com.sunday.aomicaichang.ui.common.WebViewActivity;
import com.sunday.member.entity.Member;
import com.sunday.member.utils.SignUtil;

import java.math.BigDecimal;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by yun_sheng on 2017/6/1.
 */

public class SignActivity extends BaseActivity {
    @Bind(R.id.rule)
    ImageView rule;
    @Bind(R.id.avator)
    CircleImageView avator;
    @Bind(R.id.tv_name)
    TextView tvName;
    @Bind(R.id.tv_integral)
    TextView tvIntegral;
    @Bind(R.id.tv_integral_num)
    TextView tvIntegralNum;
    @Bind(R.id.btn_mall)
    TextView btnMall;
    @Bind(R.id.btn_sign)
    ImageView btnSign;
    @Bind(R.id.status1)
    TextView status1;
    @Bind(R.id.tv_status1)
    TextView tvStatus1;
    @Bind(R.id.status0)
    TextView status0;
    @Bind(R.id.tv_status0)
    TextView tvStatus0;

    private int pageSize = 20;
    private int pageNumber = 1;
    private Member member;
    private BigDecimal score;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign);
        ButterKnife.bind(this);
        member= (Member) SharePerferenceUtils.getIns(mContext).getOAuth();
        getData();
    }

    private void getData(){
        if(member.getImage()!=null)
        Picasso.with(mContext)
                .load(member.getImage())
                .error(R.mipmap.ic_avator)
                .into(avator);
        tvName.setText(member.getNickName()+"");
        String nonce = MD5Utils.random();
        String size = String.valueOf(pageSize);
        String number = String.valueOf(pageNumber);
        String[] str1 = new String[]{"type", "pageSize", "pageNumber", "nonce"};
        String[] str2 = new String[]{"1", size, number, nonce};
        showLoadingDialog(0);
        Call<ResultDO<Score>> call = ApiClient.getApiAdapter().getScore(1, pageSize, pageNumber,
                nonce, SignUtil.getSign(str1, str2));
        call.enqueue(new Callback<ResultDO<Score>>() {
            @Override
            public void onResponse(Call<ResultDO<Score>> call, Response<ResultDO<Score>> response) {
                dissMissDialog();
                if (isFinish || response.body() == null) {
                    return;
                }
                ResultDO<Score> resultDO = response.body();
                if (resultDO.getCode() == 0) {
                    score=resultDO.getResult().getTotalScore();
                    tvIntegralNum.setText(score+"");
                    if(resultDO.getResult().getIsSign()==0){
                        status1.setVisibility(View.VISIBLE);
                        tvStatus1.setVisibility(View.VISIBLE);
                        tvStatus1.setText("+"+resultDO.getResult().getSignScore()+"");
                        status0.setVisibility(View.GONE);
                        tvStatus0.setVisibility(View.GONE);
                        btnSign.setEnabled(true);
                    }else {
                        btnSign.setEnabled(false);
                        status1.setVisibility(View.GONE);
                        tvStatus1.setVisibility(View.GONE);
                        tvStatus0.setText("明日+"+resultDO.getResult().getSignScore()+"");
                        status0.setVisibility(View.VISIBLE);
                        tvStatus0.setVisibility(View.VISIBLE);
                    }
                } else {
                    ToastUtils.showToast(mContext, resultDO.getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO<Score>> call, Throwable t) {
                dissMissDialog();
                ToastUtils.showToast(mContext, R.string.network_error);
            }
        });
    }
    @OnClick({R.id.rule,R.id.btn_mall,R.id.btn_sign})
    void onClick(View v){
        switch (v.getId()){
            case R.id.rule:
                intent = new Intent(mContext, WebViewActivity.class);
                intent.putExtra("url", ApiClient.API_URL1+"sunday/mobile/advertisement/html5ByZoneCode?zoneKey=score_rule" );
                intent.putExtra("title","积分规则");
                startActivity(intent);
                break;
            case R.id.btn_mall:
                intent=new Intent(mContext, MallActivity.class);
                startActivity(intent);
                break;
            case R.id.btn_sign:
                String nonce = MD5Utils.random();
                String[] str1 = new String[]{ "nonce"};
                String[] str2 = new String[]{ nonce};
                showLoadingDialog(0);
                Call<ResultDO> call = ApiClient.getApiAdapter().sign(
                        nonce, SignUtil.getSign(str1, str2));
                call.enqueue(new Callback<ResultDO>() {
                    @Override
                    public void onResponse(Call<ResultDO> call, Response<ResultDO> response) {
                        dissMissDialog();
                        if (isFinish || response.body() == null) {
                            return;
                        }
                        ResultDO resultDO = response.body();
                        if (resultDO.getCode() == 0) {
                            ToastUtils.showToast(mContext, "签到成功");
                            getData();
                        } else {
                            ToastUtils.showToast(mContext, resultDO.getMessage());
                        }
                    }

                    @Override
                    public void onFailure(Call<ResultDO> call, Throwable t) {
                        dissMissDialog();
                        ToastUtils.showToast(mContext, R.string.network_error);
                    }
                });
                break;
        }
    }
}
